﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;

namespace Framework
{
    public class LuaMonoBehaviour : MonoBehaviour
    {
        public LuaTable LuaComContainer
        {
            get
            {
                return _comContainer;
            }
        }
        private LuaTable _comContainer;

        private LuaFunction _onEnable;
        private LuaFunction _onDisable;
        private LuaFunction _onStart;
        private LuaFunction _onUpdate;
        private LuaFunction _onDestroy;
        private LuaFunction _onTriggerEnter;
        private LuaFunction _onTriggerExit;

        #if UNITY_EDITOR
        [LuaInterface.NoToLua]
        public List<string> luaCompNames = new List<string>();
        private LuaFunction _getCompNames;
        private int _frameCount = 0;
        #endif

        public void SetLuaComContainer(LuaTable container)
        {
            _comContainer = container;
            if(null == container)
            {
                Logger.LogError("LuaMonobehavier.SetLuaComContainer, container can not be null!");
                return;
            }
            
            _onEnable = container.GetLuaFunction("onEnable");
            _onDisable = container.GetLuaFunction("onDisable");
            _onStart = container.GetLuaFunction("onStart");
            _onUpdate = container.GetLuaFunction("onUpdate");
            _onDestroy = container.GetLuaFunction("onDestroy");
            _onTriggerEnter = container.GetLuaFunction("onTriggerEnter");
            _onTriggerExit = container.GetLuaFunction("onTriggerExit");

#if UNITY_EDITOR
            _getCompNames = container.GetLuaFunction("getCompNames");
#endif
        }

        /// <summary>
        /// 判定当前组件是否是enable的
        /// </summary>
        /// <returns></returns>
        public bool IsEnabled()
        {
            return gameObject.activeInHierarchy && this.enabled;
        }

        void OnEnable()
        {
            if (null != _onEnable)
            {
                // Logger.Log("LuaMonobehavier OnEnable --------------- ");
                _onEnable.Call(_comContainer);
            }
        }
        
        void OnDisable()
        { 
            if (null != _onDisable)
            {
                // Logger.Log("LuaMonobehavier OnDisable --------------- ");
                _onDisable.Call(_comContainer);
            }
        }

        void Start()
        {   
            if(null != _onStart)
            {
                // Logger.Log("LuaMonobehavier Start --------------- ");                
                _onStart.Call(_comContainer);
            }
        }
        
        void Update()
        {
            if(null != _onUpdate)
            {
                // Logger.Log("LuaMonobehavier Update --------------- ");
                _onUpdate.Call(_comContainer);
            }
        }

        #if UNITY_EDITOR
        void LateUpdate()
        {
            if(null != _getCompNames)
            {
                _frameCount++;
                if(_frameCount % 10 != 0)
                {
                    return;
                }
                LuaTable namesTable = _getCompNames.Invoke<LuaTable, LuaTable>(_comContainer);
                LuaArrayTable luarray = namesTable.ToArrayTable();
                luaCompNames.Clear();
                using (var iter = luarray.GetEnumerator())
                {
                    while (iter.MoveNext())
                    {
                        luaCompNames.Add(iter.Current.ToString());
                    }
                }
            }
        }
        #endif
        
        void OnTriggerEnter(Collider other)
        {
            if(null != _onTriggerEnter)
            {
                // Logger.Log("LuaMonobehavier OnTriggerEnter --------------- ");
                _onTriggerEnter.Call(_comContainer, other);
            }
        }

        void OnTriggerExit(Collider other)
        { 
            if(null != _onTriggerExit)
            {
                // Logger.Log("LuaMonobehavier OnTriggerExit --------------- ");
                _onTriggerExit.Call(_comContainer, other);
            }
        }

        void OnDestroy()
        {
            if(null != _onDestroy)
            {
                // Logger.Log("LuaMonobehavier OnDestroy --------------- ");
                _onDestroy.Call(_comContainer);
                _Clear();
            }
        }

        private void _Clear()
        {
            _comContainer = null;
            _onEnable = null;
            _onDisable = null;
            _onStart = null;
            _onUpdate = null;
            _onDestroy = null;
            _onTriggerEnter = null;
            _onTriggerExit = null;
        #if UNITY_EDITOR
            luaCompNames = null;
            _getCompNames = null;
            _frameCount = 0;
        #endif
        }
    }
}
